#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/7/6 14:47
# @Author : linjinting
# @Project : tableview-model
# @File : demo1.py
# @Software: PyCharm


import sys, os
from PySide2.QtWidgets import (QApplication, QWidget, QHeaderView,
                               QTableView, QTreeView, QSplitter, QVBoxLayout)
from tablemodel import TableModel, SortProxyModel


class DemoModelView(QWidget):
    def __init__(self, parent=None):
        super(DemoModelView, self).__init__(parent)

        # 设置窗口标题
        self.setWindowTitle('实战PyQt5: Model-View框架演示')
        # 设置窗口大小
        self.resize(480, 320)

        self.initUi()

    def initUi(self):
        vLayout = QVBoxLayout(self)
        # splitter = QSplitter(self)

        # # 创建文件系统模型
        # fsm = QFileSystemModel()
        # fsm.setRootPath(QDir.currentPath())
        # # 树状视图
        # tv = QTreeView(splitter)
        # tv.setModel(fsm)
        # tv.setRootIndex(fsm.index(QDir.currentPath()))
        # 表格视图
        data = [
            ['123450', 'ceshi22', '20200711', '已完'],
            ['123451', 'ceshi00', '20200711', '已完成'],
            ['123452', 'ceshi11', '20200711', '已完成'],
            ['123453', 'ceshi13', '20200711', '已完成'],
            ['123454', 'ceshi17', '20200711', '已完成'],
        ]
        header = ['id', 'name', 'time']
        model = TableModel()
        model.initData(data, header)

        # 新建一个排序代理
        proxy = SortProxyModel()
        # 设置代理的模型
        proxy.setSourceModel(model)
        # 开启动态排序与过滤
        proxy.setDynamicSortFilter(True)
        # 从第0行开始排序
        proxy.sort(0)

        tableView = QTableView()
        tableView.setModel(proxy)
        tableView.resizeColumnsToContents()
        tableView.horizontalHeader().setSectionResizeMode(model.columnCount()-1, QHeaderView.Stretch)
        # tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive)

        tableView.setSortingEnabled(True)
        # lv.show()
        # lv.setRootIndex(fsm.index(QDir.currentPath()))

        vLayout.addWidget(tableView)
        self.setLayout(vLayout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = DemoModelView()
    window.show()
    sys.exit(app.exec_())
